package org.cargloss.cp.controller.modul.upload;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Mode;

import com.opensymphony.xwork2.ActionSupport;

public class UploadActionStream extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String modul;
	private String CKEditorFuncNum;
	private InputStream inputStream;

	public InputStream getInputStream() {
		return inputStream;
	}

	public String execute() {

		try {

			ServletContext context = ServletActionContext.getServletContext();
			String filePath = context.getRealPath("/") + "protected/"
					+ getModul();
			System.out.println(filePath);
			BufferedImage originalImage = ImageIO.read(upload);

			int dW = originalImage.getWidth() / 4;
			int dH = originalImage.getHeight() / 4;

			BufferedImage smallImage = Scalr.resize(originalImage,
					Mode.AUTOMATIC, dW, dH);

			File destFile = new File(filePath + "/small_" + uploadFileName);
			System.out.println(CKEditorFuncNum);
			ImageIO.write(smallImage, "jpg", destFile);
			File fileToCreate = new File(filePath, uploadFileName);
			FileUtils.copyFile(this.upload, fileToCreate);
			String contextPath = context.getContextPath();
			String imgUrl = contextPath + "/protected/photos/" + uploadFileName;
			String msg = "Upload Success";
			String streamtext = "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('"
					+ getCKEditorFuncNum()
					+ "','"
					+ imgUrl
					+ "','"
					+ msg
					+ "');</script>";

			try {
				inputStream = new ByteArrayInputStream(
						streamtext.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String getModul() {
		return modul;
	}

	public void setModul(String modul) {
		this.modul = modul;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getCKEditorFuncNum() {
		return CKEditorFuncNum;
	}

	public void setCKEditorFuncNum(String cKEditorFuncNum) {
		CKEditorFuncNum = cKEditorFuncNum;
	}

}
